package com.taptech.doufu.ui.adapter.personalcenter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.bean.UserBean;
import com.taptech.doufu.ui.activity.weex.StartWeexActivityUtils;
import com.taptech.doufu.ui.activity.BrowseActivity;
import com.taptech.doufu.ui.adapter.BaseListAdapter;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.bean.personalcenter.PersonalCardInfo;
import com.taptech.doufu.ui.view.UserGradeView;
import com.taptech.doufu.util.ImageManager;
import com.taptech.doufu.ui.view.NetworkImageView;

public class PersonalSendLetterAdapter extends BaseListAdapter {

    private String type;
    private Context context;

    public PersonalSendLetterAdapter(Context context) {
        // TODO Auto-generated constructor stub
        this.context = context;
    }

    @Override
    public View getView(int position, View convertView, final ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView =  LayoutInflater.from(parent.getContext()).inflate(R.layout.personal_center_adapter_send_letter, null);
            holder = new ViewHolder();
            holder.userLabel = (TextView) convertView.findViewById(R.id.personal_center_send_letter_label);
            holder.userHeader = (NetworkImageView) convertView.findViewById(R.id.personal_center_send_letter_icon);
            holder.userHeader.setFullScreen(false);
            holder.userHeader.setRound(true);
            holder.addView = convertView.findViewById(R.id.personal_center_send_letter_add_view);
            holder.layUserGrade = convertView.findViewById(R.id.layUserGrade);
            holder.addBtn = (ImageView) convertView.findViewById(R.id.personal_center_send_letter_add_btn);

            holder.addText = (TextView) convertView.findViewById(R.id.personal_center_send_letter_add_text);
            convertView.setTag(holder);
        }
        initData(convertView, position);
        return convertView;
    }

    private void initData(final View convertView, final int position) {
        try {

            final PersonalCardInfo info = (PersonalCardInfo) getItem(position);
            if (info == null)
                return;
            final ViewHolder holder = (ViewHolder) convertView.getTag();
            if (Constant.managerIdSet.contains(info.getUid())) {
                holder.addText.setText("反馈");
            }
            holder.userHeader.setImageResource(R.drawable.default_user_portrait);
            ImageManager.displayImage(holder.userHeader, info.getUser_head_img(), 0);
            holder.layUserGrade.setData(PersonalCardInfo.getUserBean(info),false);
            holder.userLabel.setText(info.getUser_signature());

            holder.addView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (Constant.managerIdSet.contains(info.getUid())) {
                        Intent intent = new Intent(v.getContext(), BrowseActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.putExtra(Constant.URL, "http://api.doufu.la/index.php/help/question");
                        intent.putExtra(Constant.TITLE, "");
                        v.getContext().startActivity(intent);
                    } else {
                        StartWeexActivityUtils.startPrivateMsgActivity(v.getContext(),info.getUid(),
                                info.getNickname());
                    }


                }
            });
            if (type.equals("fromNewUgc")) {
                holder.addView.setVisibility(View.GONE);
                convertView.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Intent intent = new Intent();
                        intent.putExtra("user_id", info.getUid());
                        intent.putExtra("user_nikename", info.getNickname());
                        ((Activity) context).setResult(Constant.AT_RESULI, intent);
                        ((Activity) context).finish();
                    }
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected class ViewHolder {
        NetworkImageView userHeader;
        ImageView addBtn;
        TextView  userLabel, addText;
        View addView;
        UserGradeView layUserGrade;
    }

    public void setType(String type) {
        // TODO Auto-generated method stub
        this.type = type;
    }

}
